package ch.elektroplan.christian.projectFinder.presentation;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import sun.net.www.content.image.jpeg;

import ch.elektroplan.christian.projectFinder.business.ProjectFinder;

public class ChooseDirectoriesFrame extends SubFrames {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ProjectFinder projectFinder;
	private JFrame parent;
	private JButton cancel, save, adddir, remdir;
	private JList list;
	private DefaultListModel listModel;

	public ChooseDirectoriesFrame(ProjectFinder projectFinder, JFrame parent) {
		this.projectFinder = projectFinder;
		this.parent = parent;

		this.setLocation(parent.getLocation());
		this.setSize(600, 250);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.parent.setEnabled(false);

		this.list = new JList();
		this.listModel = new DefaultListModel();
		this.list.setModel(listModel);

		this.cancel = new JButton("cancel");
		this.cancel.addActionListener(new Buttonlistener());

		this.save = new JButton("save");
		this.save.addActionListener(new Buttonlistener());

		this.adddir = new JButton("add");
		this.adddir.addActionListener(new Buttonlistener());

		this.remdir = new JButton("remove");
		this.remdir.addActionListener(new Buttonlistener());

		this.add(new ButtonPanel(), BorderLayout.SOUTH);
		this.add(new DirectoryButtonHelpPanel(), BorderLayout.EAST);

		this.add(new ListClass(), BorderLayout.CENTER);

		this.setVisible(true);
	}

	private class Buttonlistener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(cancel)) {

				if (JOptionPane.showConfirmDialog(ChooseDirectoriesFrame.this,
						"Settings wont be saved!", "Info",
						JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
					closeAndrewake(parent);

				}
			} else if (e.getSource().equals(save)) {
				save(projectFinder, projectFinder.getConfigfilePF()
						.getSearchDirectories());
				closeAndrewake(parent);
			} else if (e.getSource().equals(adddir)) {
				File f = null;
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				if (chooser.showOpenDialog(ChooseDirectoriesFrame.this) == JFileChooser.APPROVE_OPTION) {
					f = chooser.getSelectedFile();
				}
				if (f != null) {

					if (f.isDirectory() && f.canExecute()) {

						projectFinder.getConfigfilePF().getSearchDirectories()
								.add(f);
						listModel.addElement(f);
						projectFinder.refreshProjectlist();

					} else {
						JOptionPane.showMessageDialog(
								ChooseDirectoriesFrame.this,
								"choose a directory", "Error",
								JOptionPane.WARNING_MESSAGE);
					}
				}
			} else if (e.getSource().equals(remdir)) {

				if (list.getSelectedValue() != null) {

					ArrayList<File> templist = new ArrayList<File>();
					templist = projectFinder.getConfigfilePF()
							.getSearchDirectories();
					templist.remove(list.getSelectedValue());
					projectFinder.getConfigfilePF().setSearchDirectories(
							templist);
					projectFinder.refreshProjectlist();
					listModel.removeElement(list.getSelectedValue());


				} else {
					JOptionPane.showMessageDialog(ChooseDirectoriesFrame.this,
							"Select a Value");
				}

			}
		}

	}

	private class ButtonPanel extends JPanel {

		public ButtonPanel() {
			setLayout(new FlowLayout());
			add(ChooseDirectoriesFrame.this.save);
			//add(ChooseDirectoriesFrame.this.cancel);
		}
	}

	private class DirectoryButtonPanel extends JPanel {

		public DirectoryButtonPanel() {

			setLayout(new GridLayout(2, 1));
			add(adddir);
			add(remdir);

		}
	}

	private class DirectoryButtonHelpPanel extends JPanel {

		public DirectoryButtonHelpPanel() {
			setLayout(new BorderLayout());
			add(new DirectoryButtonPanel(), BorderLayout.NORTH);
		}
	}

	private class ListClass extends JPanel {
		public ListClass() {
			list.setPreferredSize(new Dimension(480, 160));

			for (int i = 0; i < projectFinder.getConfigfilePF()
					.getSearchDirectories().size(); i++) {

				listModel.addElement(projectFinder.getConfigfilePF()
						.getSearchDirectories().get(i));
			}
			add(list);

		}
	}

}
